<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\index\controller;

use app\common\controller\Base;
use utils\GeetestLib;
use think\Db;

/**
 * @title No17专有模块
 * 
 */
class No17 extends Base {

    /**
     * @title 默认页面
     */
    public function index() {
        
    }

    /**
     * @title 品牌大片
     */
    public function video() {
        return view();
    }

    /**
     * @title 抢购
     */
    public function snapbuy() {



        $this->error('排队中');
    }

    /**
     * @title 抢购的状态
     */
    public function snapbuy_status() {

        if (date('d') % 2 == 0) {
            $qgtime = date('Y-m-d 10:17:00');
        } else {
            $qgtime = date("Y-m-d 10:17:00", strtotime("+1 day"));
        }


        if (time() - strtotime($qgtime) > 300) {


            $this->error('已售磬');
        }
    }

    /**
     * @title 经销商所覆盖的城市
     */
    public function citys() {






        $r1 = Db::name('jingxiaoshang')->where(' province <> "" ')->column('province');
        $r2 = Db::name('jingxiaoshang')->where(' city <> "" ')->column('city');
        //   $r3 = Db::name('jingxiaoshang')->where(' area <> "" ')->getField('area', true);


        $r_all = array_merge($r1, $r2);



        $c = array_intersect(config('citys'), $r_all);


        //print_r($c);exit;
        //print_r($r2);exit;
        //$result = array_intersect_key($citys, $c);


        echo json_encode($c);

        //  print_r();
    }

    /**
     * @title 经销商查询
     */
    public function dealer() {


        $this->assign('seo', ['title' => 'NO17品牌经销商查询', 'keywords' => '', 'description' => '']);


        $var = input('get.');
        //print_r($var);
        $province = input('get.province', '');
        $city = input('get.city', '');
        $dealer = input('get.dealer', '');
        $productids = input('get.productids', '');


        $wheres = array();
        if ($province) {
            $wheres['province'] = array('like', '%' . $province . '%');
        }
        if ($city) {
            $wheres['city'] = array('like', '%' . $city . '%');
        }
        if ($dealer) {
            $wheres['company'] = array('LIKE', '%' . $dealer . '%');
        }
        if ($productids) {
            $wheres['product_ids'] = array('LIKE', '%' . $productids . '%');
        }
        if ($province) {
            $result = Db::name('jingxiaoshang')->where($wheres)->select();
        } else {
            $result = [];
        }



        $this->assign('province', $province);
        $this->assign('city', $city);
        //  $this->assign('productids', $productids);

        $this->assign('productids', $productids);

        $this->assign('result', $result);

        return view();
    }

    /**
     * @title V码换购
     */
    public function vcode() {
        if (request()->isPost()) {
            $vcode = input('post.vcode');
            $captcha = input('post.captcha');

            if (trim($vcode) == '') {
                $this->error('V码不能为空');
            } elseif (trim($captcha) == '') {
                $this->error('验证码不能为空');
            } elseif (!captcha_check($captcha)) {
                $this->error('验证码有误');
            }

            $result = Db::name('vcode')->where('md5="' . md5($vcode) . '" ')->find();
            if ($result) {
                //未激活
                if ($result['status'] == 0) {
                    $this->error('你输入的V码无效');
                } elseif ($result['status'] == 1) {
                    //未使用
                    session('vcode', $vcode);
                    $this->success('', url('index/no17/order'));
                } elseif ($result['status'] == 2) {
                    //已使用
                    $this->success('您的V码已被使用', '');
                }
            } else {
                $this->error('你输入的V码无效');
            }
        } else {

            $this->assign('seo', [
                'title' => config('base.title'),
                'keywords' => config('base.keywords'),
                'description' => config('base.description'),
            ]);

            return view();
        }
    }

    /**
     * @title 下单页面
     */
    public function order() {
        
        $vcode = session('vcode');
        
        if (request()->isPOST()) {
            //$post = input('post.');
            $data['pay_subject'] = 'NO17典藏版';
            $data['total'] = '1998';
            $data['shipping_name'] = input('post.shipping_name', '');
            $data['shipping_tel'] = input('post.shipping_tel', '');
            $data['shipping_address_detail'] = input('post.shipping_address_detail', '');
            $data['shipping_province'] = input('post.province', '');
            $data['shipping_city'] = input('post.city', '');
            $data['shipping_county'] = input('post.area', '');

            $data['remark'] = input('post.remark', '下单');
            $data['vcode'] = $vcode;
            $data['status'] = 20; // 用码换购代表已经付过钱了

            if (trim($data['shipping_name']) == '') {
                $this->error('收件人不能为空');
            } elseif (trim($data['shipping_tel']) == '') {
                $this->error('收件人电话不能为空');
            } elseif (trim($data['shipping_province']) == '') {
                $this->error('请选择所属地区');
            } elseif (trim($data['shipping_address_detail']) == '') {
                $this->error('请填写详细地址');
            }



            $data['order_num'] = date('YmdHis');  // 订单号过于简单
            $data['ip'] = get_client_ip();
            $data['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
            $data['payment_code'] = 'V码';



            $insert_id = model('order')->tianjia($data);


            //添加一条跟踪记录 待付款
            $data2['order_id'] = $insert_id;
            $data2['order_status'] = 20; //V码换购 默认是付过钱的
            $data2['remark'] = 'V码换购：' . $vcode;
            model('order_history')->tianjia($data2);


            if (is_numeric($insert_id) && $insert_id > 0) {

                //更新V码状态
                Db::name('vcode')->where('md5', md5($vcode))->setField('status', 2);

                session('vcode', null);
                $this->success('', url('index/no17/order_result', ['id' => $insert_id]));
            } else {
                $this->error($message);
            }
        } else {
            if (!$vcode) {
                $this->error('V码不存在，请重试');
            }
            $this->assign('vcode', $vcode);


            $this->assign('seo', [
                'title' => 'V码换购NO17典藏版',
                'keywords' => '',
                'description' => ''
            ]);


            return view();
        }
    }

    /**
     * @title 订单结果 
     * @return type
     */
    public function order_result($id) {


        if (!$id)
            $this->error('参数不能为空！');

        $one = Db::name('order')->where('id', $id)->find();

        $this->assign($one);

        $this->assign('seo', [
            'title' => 'V码换购NO17典藏版',
            'keywords' => '',
            'description' => ''
        ]);

        return view();
    }

}
